package com.totoro.tyrank.executor.command.impl;

import com.totoro.tyrank.TyRank;
import com.totoro.tyrank.constants.ReloadTypeConst;
import com.totoro.tyrank.data.RankingData;
import com.totoro.tyrank.executor.command.MainCommandExecutor;
import com.totoro.tyrank.utils.ColorUtil;
import org.bukkit.command.CommandSender;

import java.util.Map;

/**
 * @author totoro
 * @description 重载命令执行器
 * @date 16:33 2025/7/31
 **/
public class ReloadCommandExecutor implements MainCommandExecutor {
    @Override
    public void execute(CommandSender sender, String[] args) {
        if(!sender.isOp()) {
            sender.sendMessage(ColorUtil.color(TyRank.getConfigManager().getConfigData().getPrefix() +
                    TyRank.getLanguageManager().getLanguageData().getMsg().getNoPermission()));
            return;
        }
        // 如果只有1那么则是reload
        if(args.length == 1) {
            TyRank.getConfigManager().reloadConfig();
            TyRank.getLanguageManager().reloadLanguage();
            TyRank.getRankingManager().reloadRank();
            TyRank.getRankDataManager().loadRankData();
            TyRank.getRankingTaskManager().reloadRankingsTask();
            TyRank.getPlayerDataManager().loadRankPlayerData();
            sender.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                    ColorUtil.color("&e所有配置文件全部重载成功！"));
            return;
        }

        // 如果是2那么则可能是刷新配置或者单个排行榜
        if(args.length == 2) {
            String reloadType = args[1];
            if(reloadType.equals(ReloadTypeConst.CONFIG)) {
                TyRank.getConfigManager().reloadConfig();
                sender.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                        ColorUtil.color("&e基础配置重载成功！"));
            } else if (reloadType.equals(ReloadTypeConst.LANGUAGE)) {
                TyRank.getLanguageManager().reloadLanguage();
                sender.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                        ColorUtil.color("&e语言文件重载成功"));
            } else {
                // 如果输入的不是以上两个，那就只能是刷新单个排行榜了
                Map<String, RankingData> rankDataMap = TyRank.getRankingManager().getRankDataMap();
                if(! rankDataMap.containsKey(reloadType)) {
                    sender.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                            ColorUtil.color("&e该排行榜不存在！"));
                    return;
                }
                RankingData rankingData = rankDataMap.get(reloadType);
                TyRank.getRankingManager().reloadRank(rankingData.getId());
                TyRank.getRankDataManager().loadRankData(rankingData.getId());
                TyRank.getRankingTaskManager().reloadRanking(rankingData.getId());
                TyRank.getPlayerDataManager().loadRankPlayerData(rankingData.getId());
                sender.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                        String.format(ColorUtil.color("&eId为 &c[%s] &e的排行榜重载成功！"), rankingData.getId()));
            }
        }
    }
}
